home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / amhelios / win_bmap.h < prev    next >
C/C++ Source or Header  |  1999-01-01  |  7KB  |  229 lines

  1. ////////////////////////////////////////////////////////////
  2. //
  3. //  WIN_BMAP.H - MS-Windows Bitmap Class Include File
  4. //
  5. //  Version:    1.03A
  6. //
  7. //  History:    94/08/23 - Version 1.00A release.
  8. //              94/12/02 - General rewrite.
  9. //              94/12/16 - Version 1.01A release.
  10. //              94/12/17 - Deleted __huge conditional
  11. //                         directive.
  12. //              94/12/19 - Merged GetPalScanWidth and
  13. //                         GetRGBScanWidth functions into
  14. //                         GetScanWidth.
  15. //              95/02/05 - Version 1.02A release.
  16. //              95/03/21 - Made GetPixel function public.
  17. //              95/03/23 - Added FlushBitmap function
  18. //                         prototype.
  19. //              95/06/24 - Made GetPixel, SetPalPixel and
  20. //                         SetPixel functions inline.
  21. //              95/06/30 - Added ppix data member.
  22. //                       - Added IncPixelPtr, SetPixelPtr
  23. //                         and SetCurrPixel functions.
  24. //              95/07/21 - Version 1.02B release.
  25. //              96/02/14 - Version 1.02C release.
  26. //              96/04/01 - Version 1.03A release.
  27. //
  28. //  Compilers:  Microsoft Visual C/C++ Professional V1.5
  29. //              Borland C++ Version 4.5
  30. //
  31. //  Author:     Ian Ashdown, P.Eng.
  32. //              byHeart Software Limited
  33. //              620 Ballantree Road
  34. //              West Vancouver, B.C.
  35. //              Canada V7S 1W3
  36. //              Tel. (604) 922-6148
  37. //              Fax. (604) 987-7621
  38. //
  39. //  Copyright 1994-1996 byHeart Software Limited
  40. //
  41. //  The following source code has been derived from:
  42. //
  43. //    Ashdown, I. 1994. Radiosity: A Programmer's
  44. //    Perspective. New York, NY: John Wiley & Sons.
  45. //
  46. //  It may be freely copied, redistributed, and/or modified
  47. //  for personal use ONLY, as long as the copyright notice
  48. //  is included with all source code files.
  49. //
  50. ////////////////////////////////////////////////////////////
  51.  
  52. #ifndef _WIN_BMAP_H
  53. #define _WIN_BMAP_H
  54.  
  55. #include "oct_quan.h"
  56.  
  57. // Round number upwards to next multiple of four
  58. #define BM_WidthBytes(i) (((i + 3) / 4) * 4)
  59.  
  60. // Number of bytes per pixel (24-bit RGB)
  61. static const int BM_RGB_BPP = 3;
  62.  
  63. // File read/write block size
  64. static const WORD BM_BlockSize = 0x8000;
  65.  
  66. // Number of palette elements
  67. //
  68. // NOTE: MS-Windows reserves 20 static colors for its
  69. //       default palette. Limiting the BMP file color
  70. //       palette to 236 colors ensures that these colors
  71. //       will not be affected when the BMP file is
  72. //       displayed.
  73.  
  74. static const int BM_PaletteSize = 236;
  75.  
  76. class WinBitmap : public OctQuant       // MS-Windows bitmap
  77. {
  78.   private:
  79.     BITMAPFILEHEADER bm_file;   // DIB file header
  80.  
  81.     // NOTE: the following data structures MUST be
  82.     //       contiguous and aligned on WORD boundaries
  83.  
  84. #pragma pack(2)     // Enable WORD alignment
  85.     // DIB information header pointer
  86.     BITMAPINFOHEADER bm_iheader;
  87.  
  88.     // DIB bitmap palette (for palette-mapped DIBs only)
  89.     RGBQUAD bm_colors[BM_PaletteSize];
  90. #pragma pack()      // Revert to default compiler alignment
  91.  
  92.     BOOL pal_flag;      // Display palette flag
  93.     BOOL rgb_flag;      // 24-bit RGB color flag
  94.     BYTE __huge *pbm;   // Bitmap pointer
  95.     BYTE __huge *ppix;  // Pixel pointer
  96.     DWORD pal_scan;     // Palette-mapped scanline width
  97.     DWORD pal_size;     // Palette-mapped bitmap size
  98.     DWORD rgb_scan;     // RGB scanline width
  99.     DWORD rgb_size;     // RGB bitmap size
  100.     HBITMAP hddb;       // DDB bitmap handle
  101.     HGLOBAL hdib;       // DIB bitmap handle
  102.     HPALETTE hpal;      // Logical palette handle
  103.                             
  104.     BOOL AllocBitmap();
  105.     BOOL AllocPalette();
  106.     BOOL DisplayDDB( HDC, POINT &, RECT & );
  107.     BOOL WriteBitmap( int, DWORD );
  108.     void FreeBitmap();
  109.     void InitDIB();
  110.     void SetDIBPalette();
  111.  
  112.     // Write 256-color pixel to bitmap
  113.     void SetPalPixel( int x, int y, BYTE &c )
  114.     {
  115.       BYTE __huge *ppixel;  // Pixel pointer
  116.  
  117.       // Get pixel pointer
  118.       ppixel = pbm + (y * pal_scan) + x;
  119.  
  120.       *ppixel = c;  // Set pixel color palette index
  121.     }
  122.  
  123.   public:
  124.     WinBitmap()
  125.     {
  126.       pbm = NULL;
  127.       hddb = NULL;
  128.       hdib = NULL;
  129.       hpal = NULL;
  130.       width = height = 0;
  131.       pal_scan = rgb_scan = (DWORD) 0L;
  132.       pal_size = rgb_size = (DWORD) 0L;
  133.       max_colors = BM_PaletteSize;
  134.  
  135.       bm_file.bfType = 0x4d42;  // 'BM' signature
  136.       bm_file.bfSize = 0L;
  137.       bm_file.bfReserved1 = 0;
  138.       bm_file.bfReserved2 = 0;
  139.  
  140.       bm_iheader.biSize = (DWORD) sizeof(BITMAPINFOHEADER);
  141.       bm_iheader.biWidth = 0L;
  142.       bm_iheader.biHeight = 0L;
  143.       bm_iheader.biPlanes = 1;
  144.       bm_iheader.biCompression = BI_RGB;
  145.       bm_iheader.biSizeImage = 0L;
  146.       bm_iheader.biXPelsPerMeter = 0L;
  147.       bm_iheader.biYPelsPerMeter = 0L;
  148.  
  149.       pal_flag = FALSE;
  150.       rgb_flag = TRUE;
  151.     }
  152.  
  153.     ~WinBitmap()
  154.     {
  155.       FreeBitmap();
  156.       FreePalette();
  157.     }
  158.  
  159.     BOOL Display( HDC, POINT &, RECT & );
  160.     BOOL GetPaletteFlag() { return pal_flag; }
  161.     BOOL Open( int, int );
  162.     BOOL QuantizeColors();
  163.     BOOL Write( char * );
  164.     BYTE __huge *GetBitmapPtr() { return pbm; }
  165.     ColorRGB *GetPalettePtr() { return palette; }
  166.     int GetHeight() { return height; }
  167.     int GetRGBFlag() { return rgb_flag; }
  168.     int GetScanWidth()
  169.     {
  170.       if (rgb_flag == TRUE)
  171.         return (int) rgb_scan;
  172.       else
  173.         return (int) pal_scan;
  174.     }
  175.     int GetWidth() { return width; }
  176.     void Close();
  177.     void FlushBitmap();
  178.     void FreePalette();
  179.  
  180.     // Read 24-bit RGB pixel from bitmap
  181.     void GetPixel( int x, int y, ColorRGB *pc )
  182.     {
  183.       BYTE __huge *ppixel;  // Pixel pointer
  184.  
  185.       // Get pixel pointer
  186.       ppixel = pbm + (y * rgb_scan) + (x * BM_RGB_BPP);
  187.  
  188.       // Get pixel colors (NOTE REVERSED ORDER!)
  189.       pc->SetBlue(ppixel[0]);
  190.       pc->SetGreen(ppixel[1]);
  191.       pc->SetRed(ppixel[2]);
  192.     }
  193.  
  194.     void IncPixelPtr() { ppix += BM_RGB_BPP; }
  195.     void Redisplay( HWND );
  196.  
  197.     // Write current 24-bit RGB pixel to bitmap
  198.     void SetCurrPixel( ColorRGB &c )
  199.     {
  200.       // Set pixel colors (NOTE REVERSED ORDER!)
  201.       ppix[0] = c.GetBlue();
  202.       ppix[1] = c.GetGreen();
  203.       ppix[2] = c.GetRed();
  204.     }
  205.  
  206.     void SetPaletteFlag( BOOL f ) { pal_flag = f; }
  207.  
  208.     // Write 24-bit RGB pixel to bitmap
  209.     void SetPixel( int x, int y, ColorRGB &c )
  210.     {
  211.       BYTE __huge *ppixel;  // Pixel pointer
  212.  
  213.       // Get pixel pointer
  214.       ppixel = pbm + (y * rgb_scan) + (x * BM_RGB_BPP);
  215.  
  216.       // Set pixel colors (NOTE REVERSED ORDER!)
  217.       ppixel[0] = c.GetBlue();
  218.       ppixel[1] = c.GetGreen();
  219.       ppixel[2] = c.GetRed();
  220.     }
  221.  
  222.     void SetPixelPtr( int x, int y )
  223.     { ppix = pbm + (y * rgb_scan) + (x * BM_RGB_BPP); }
  224.     void SetRGBFlag( int r ) { rgb_flag = r; }
  225. };
  226.  
  227. #endif
  228.  
  229.